package com.jevons.geo_edit.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;


import com.jevons.geo_edit.R;
import com.jevons.geo_edit.databinding.FragmentGisMapLayerGeometryListBinding;
import com.jevons.geo_edit.model.GisField;
import com.jevons.geo_lib.geojson.MapboxJsonHelper;
import com.jevons.muffin.fragment.BaseBindingFragment;
import com.jevons.muffin.utilcode.StringUtils;
import com.jevons.muffin.vm.BasePresenter;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Copyright 2020 JxGIS
 *
 * @file GisLayerGeometryListFragment
 * @auth linaisheng
 * Created on 2024/4/9.
 * Description：
 */
public class GisLayerGeometryListFragment extends BaseBindingFragment<FragmentGisMapLayerGeometryListBinding, BasePresenter> {

    private OnClickListener clickListener;

    private String layerId;
    private ArrayList<GisField> fieldList;
    private List<Feature> featureList = new ArrayList<>();

    private boolean canEdit = false;
    private int selectedRow = -1;

    @Override
    public int getContentView() {
        return R.layout.fragment_gis_map_layer_geometry_list;
    }

    @Override
    public void onInitView(Bundle savedInstanceState) {
        dataBinding.setClickHandler(new ClickHandler());

        Bundle bundle = getArguments();

        layerId = bundle.getString("layerId");
        fieldList = bundle.getParcelableArrayList("fieldList");
        String geoJson = bundle.getString("featureCollectionJson", "");
        if (StringUtils.isNotEmpty(geoJson)) {
            FeatureCollection featureCollection = FeatureCollection.fromJson(geoJson);
            featureList.addAll(featureCollection.features());
        }

        initData();
    }


    private void initData() {
//        List<Object> propertyList = new ArrayList<>();
//
//        if (fieldList != null && fieldList.size() > 0) {
//            for (int i = 0; i < featureList.size(); i++) {
//                Feature feature = featureList.get(i);
//                LinkedHashMap<String, String> dataMap = new LinkedHashMap<>();
//                dataMap.put("序号", Integer.toString(i +1));
//                for (GisField field : fieldList) {
//                    String value = feature.getStringProperty(field.getId());
//                    if (value == null) {
//                        value = "";
//                    }
//                    if (field.getFieldType().equals(EditPropertyAdapter.FIELD_TYPE_DATA)) {
//                        if (value.contains(" ")) {
//                            value = value.split(" ")[0];
//                        }
//                    }
//                    dataMap.put(field.getFieldName(), value);
//                }
//                propertyList.add(dataMap);
//            }
//        } else if (featureList.size() > 0) {
//            for (int i = 0; i < featureList.size(); i++) {
//                LinkedHashMap<String, String> dataMap = new LinkedHashMap<>();
//                dataMap.put("序号", Integer.toString(i +1));
//                propertyList.add(dataMap);
//            }
//        }
//
//        if (propertyList.size() > 0) {
//
//            MapTableData mapTableData = MapTableData.create("属性表", propertyList);
//            mapTableData.getColumns().get(0).setFixed(true);
//            mapTableData.setOnRowClickListener(new TableData.OnRowClickListener<Object>() {
//                @Override
//                public void onClick(Column column, Object o, int col, int row) {
//                    selectedRow = row;
//                    dataBinding.tableView.invalidate();
//                    if (clickListener != null) {
//                        Feature feature = featureList.get(selectedRow);
//                        clickListener.onClickFeature(layerId, feature.getStringProperty("id"));
//                    }
//                }
//            });
//            dataBinding.tableView.setTableData(mapTableData);
//        }
    }



    public class ClickHandler {

        public void clickClose() {
            if (clickListener != null) {
                clickListener.onClose();
            }
        }

        public void clickLocate() {
            if (clickListener != null && selectedRow >= 0) {
                Feature feature = featureList.get(selectedRow);
                clickListener.onLocateFeature(layerId, feature.getStringProperty("id"));
            }
        }


        public void clickDelete() {
            if (clickListener != null && selectedRow >= 0) {
                Feature feature = featureList.remove(selectedRow);
                clickListener.onDeleteFeature(layerId, feature.getStringProperty("id"));
                selectedRow = -1;
                initData();
            }
        }

    }


    public void setClickListener(OnClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public interface OnClickListener {
        
        void onClickFeature(String layerId, String featureId);

        void onLocateFeature(String layerId, String featureId);

        void onDeleteFeature(String layerId, String featureId);

        void onClose();
    }

    public static GisLayerGeometryListFragment getInstance(
            String layerId, ArrayList<GisField> fieldList, List<Feature> features, boolean canEdit) {
        GisLayerGeometryListFragment listFragment = new GisLayerGeometryListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("layerId", layerId);
        bundle.putParcelableArrayList("fieldList", fieldList);
        if (features != null && features.size() > 0) {
            bundle.putString("featureCollectionJson", MapboxJsonHelper.featureCollectionToJson(
                    FeatureCollection.fromFeatures(features)));
        }
        bundle.putBoolean("canEdit", canEdit);

        listFragment.setArguments(bundle);
        return listFragment;
    }

}